<?php

namespace app\admin\controller;

use app\admin\model\Category as ModelCategory;
use app\admin\model\Tap;
use app\BaseController;
use app\service\Tool;
use think\facade\Filesystem;
use think\facade\Session;

class Category extends Base
{
    public function index()
    {
        $this->logs('[管理员]分类视图');
        $list = ModelCategory::listTree();
        return view('index',['list' => $list]);
    }
    public function list()
    {
        $list = ModelCategory::listAll();
        return success($list, '获取成功', 0);
    }
    public function add()
    {
        $this->logs('[管理员]添加分类');
        if (request()->isPost()) {
            $param = input();
            if ($param['category'] == '') return error('分类名称不能为空');
            if ($param['pid'] != 0) {
                if (ModelCategory::findOrEmpty($param['pid'])->isEmpty()) return error('不存在父分类ID' . $param['pid']);
            }
            $res = ModelCategory::create($param);
            if ($res) return success();
            else return error();
        }
    }
      // 文章介绍图
   public function uploadImg()
   {
    $this->logs('[管理员]上传分类图片');
       $images = request()->file('img');
       return Tool::uploadImg($images , 'category');
   }
    public function getData($id)
    {
        $this->logs('[管理员]得到分类消息');
        $data = ModelCategory::find($id);
        return success($data);
    }
    public function update()
    {
        $this->logs('[管理员]更新分类');
        if (request()->isPost()) {
            $param = input();
            $data = ModelCategory::find($param['id']);
            $res = ModelCategory::update($param);
            if ($res) return success();
            else return error();
        }
    }
    public function del($id)
    {
        $this->logs('[管理员]删除分类');
        $res = (new ModelCategory())->del($id);
        if($res == 1) return success();
        return error();
    }
}
